Cache-Control Header
HTTP/1.1で追加された
RFC2616
header種別: General header
mdn
RFC7234
前提と概要
こんな感じで指定する
code:response
Cache-Control: no-store
code:response
Cache-Control: private, no-store, no-cache, must-revalidate
このprivateとかno-storeとかがどういう意味?という話
Cache-Controlを指定していない時は、Cache-Controlが未指定の時の動作になる
defaultでも条件を満たしていればcacheされる
Cache-Controlhederは、cacheを防ぐために使用するという側面もある
しかし、CDNによってはその仕様を無視するものがある
例えば、defaultではcacheしないとか、privateを付けたらcacheしないとか
直観(人による)と、HTTPの仕様と、CDNの実装にズレがあって難しいmrsekut.icon
Cache-Controlは、Resonse Header/Request Headerのいずれでも指定できる
ただし殆どの場合、Response Headerの話をしている
たまに、Request Headerでも指定するよ、という感覚で良さそうmrsekut.icon
Response HeaderのCacheの指定は、browserやProxyに対して「cacheしろ/するな」と指示している
Cacheしないresponseの指定
Response Headerに指定できるdirectives
cacheに保存する条件を指定するもの
Cache-Controlが未指定の時
Cache-Control: public
Cache-Control: private
cacheの使われ方の指定
Cache-Control: no-store
Cache-Control: no-cache
cacheの更新の方法を指定する
cacheの期限が切れた時に、どうするか?
cacheを使い続けるか?cacheを放棄するか?
放棄する場合、originがダウンしている時のことも考えているか?
Cache-Control: must-revalidate
Cache-Control: proxy-revalidate
Cache-Control: imutable
データの取扱を指定する
Cache-Control: no-transform
/mrsekut-book-4297119250/162
defaultでは、ProxyやChromeのデータセーバーなどが、通信量の削減などのために、勝手にデータを弄ることがあるので、それを防ぐための指定
TTLを指定する
Cache-Control: max-age
Cache-Control: s-maxage=<seconds>
Cache-Control: stale-while-revalidate=<seconds>
Cache-Control: stale-if-error=<seconds>
Request Headerに指定できるdirectives
Cache-Control: max-age
Cache-Control: max-stale=<seconds>
Cache-Control: min-fresh=<seconds>
Cache-Control: no-cache
Cache-Control: no-store
Cache-Control: no-transform
Cache-Control: only-if-cached
参考
/mrsekut-book-4297119250/151 ~
本に書いているのは基本的にresponse headerの話をしている
#WIP
https://zenn.dev/jxck/articles/cache-anatomia-release-note
table:_
Directive Request Response
max-age yes yes
max-stale yes
min-fresh yes
no-cache yes yes
no-store yes yes
no-transform yes yes
only-if-cached yes
must-revalidate yes
public yes
private yes
proxy-revalidate yes
s-maxage yes
no-cacheとmust-revalidateを併用する意味 #??
cacheの期間指定
cacheの期限が切れた時にどうするか?
例えば、
revalidateするが、originがdownしていたのでcacheを使い続けた
revalidateするが、それが完了するまでは古いcacheを使用した (s-w-r)
revalidateした後に、更新がなかったのでcacheを使い続けた
指定した期間保存されるとは限らない
cacheにも容量がある
期間までに、それ以上のデータが入ってくれば、元々あったcacheは押し出されて消えることになる
期限が切れたからと言って、storageから消えるとは限らない
cacheのstorageには入れておいて、次来た時の為に残すこともある
実装に依る
誰が誰に対して指示をしていて、誰がcacheをして、cacheしていない時と比べて、どこが速くなっているのか #??
request headerのCache Control
client(broser?)が、Proxyに、対して言う
Cacheが有効な状況下で、
何もしていない場合は、ProxyはCacheを返す
no-cacheとかをreqで送れば、ProxyはCacheを更新する、感じ?
/mrsekut-book-4297119250/152
いまいち何を言っているのかわからない #??
Proxyはreqからくるno-cacheは無視するので、指定してもほぼ無意味だということ?
https://stackoverflow.com/questions/14541077/why-is-cache-control-attribute-sent-in-request-header-client-to-server
この指定って意味ある?
コレとコレは併用することはありえない、というパターンがあれば例を見たい
max-ageとno-cacheは併用できない
max-ageとmust-revalidate/proxy-revalidatetは併用できる
指定する順番と意味
用語
age
cacheされ始めてからの経過時間
cacheされていない場合は、そもそもこの値は存在しない
revalidate
再検証
originに問い合わせること
cacheが最新か、古くなってないか
TTLが未定義な時は?
max-ageなどを一切指定いない時の動作
/mrsekut-book-4297119250/175
#??
react-queryのcacheもresponse headerの内容によって変わるのか?
参考
『ハイパフォーマンスWebサイト』 3章
https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.1
https://web.dev/http-cache/#cache-control-
https://qiita.com/hkusu/items/d40aa8a70bacd2015dfa
https://qiita.com/yuuuking/items/4f11ccfc822f4c198ab0
https://qiita.com/anchoor/items/2dc6ab8347c940ea4648#️-cache-control-ヘッダ
Cache-Control: must-understand
https://blog.jxck.io/entries/2021-02-12/cache-control-must-understand.html
/mrsekut-book-4297119250/155
https://github.com/httpwg/http-core/issues/120
https://github.com/httpwg/http-core/issues?q=is%3Aissue+label%3Acaching
https://asnokaze.hatenablog.com/entry/2019/11/04/213002
たぶんここじゃない
https://csswizardry.com/2019/03/cache-control-for-civilians/